//
//  MessageViewController.m
//  MyApplication
//
//  Created by 彭凯敏 on 15/7/30.
//  Copyright (c) 2015年 52xiaoluo. All rights reserved.
//

#import "MessageViewController.h"
#import "MessageCell.h"

#import "MsgLogic.h"
#import "FuseMsg.h"

#import "DataBaseSimple.h"
#import "QueueLogic.h"
@interface MessageViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong,nonatomic)NSMutableArray *dataArray;


@end

@implementation MessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createUI];
    
    _dataArray = [[NSMutableArray alloc]init];
    
    NSArray * array =[[MsgLogic shareInstance]getMsg_Array];
  
    [_dataArray setArray: array];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reloadTableView:) name:@"messageNotifi" object:nil];
}
- (void)createUI
{
    
    [self baseCtlSetTitle:@"消息"];
    [self baseCtlSetLeftBtnTitle:nil andWithRightTitle:@"接收"];
    [self.rightNavgationItem setAction:@selector(clickMessage:)];
    
    [_tableView setLineType:TableViewLineTypeAll];
}

- (void)clickMessage:(UIButton *)btn
{
    DLog("clickMessage");
    [[MsgLogic shareInstance] pullMsg];
}
- (void)reloadTableView:(NSNotification *)noti
{
    NSArray *fuseMsgArray = noti.object;
    [_dataArray setArray:fuseMsgArray];

    dispatch_async(dispatch_get_main_queue(), ^{
        [_tableView reloadData];
    });
    

    
}

#pragma mark - UITableViewDelegate & UITableViewDataSource
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MessageCell * cell = [tableView dequeueReusableCellWithIdentifier:@"MessageCell"];
    if (!cell)
    {
        [tableView registerNib:[UINib nibWithNibName:@"MessageCell" bundle:nil] forCellReuseIdentifier:@"MessageCell"];
        cell = [tableView dequeueReusableCellWithIdentifier:@"MessageCell"];
        
    }
    
    FuseMsg *fuse = [_dataArray objectAtIndex:indexPath.row];

 
    cell.seqLabel.text = [NSString stringWithFormat:@"%ld",(long)fuse.msg.msgHead.msgSeq];

    cell.timeLabel.text = [KMGetDate timeFromNum:[NSString stringWithFormat:@"%u",(unsigned int)fuse.msg.msgHead.msgTime] andFormStr:@"YYYY-MM-dd HH:mm:ss"];

    cell.recieveLabel.text = [KMGetDate timeFromNum:fuse.recieveTimeStamp andFormStr:@"YYYY-MM-dd HH:mm:ss"];
    

    
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 88;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    
}

@end
